<?php

namespace app\admin\controller;

use think\Request;

class Medicament extends Base
{

    public function _initialize() {
        parent::_initialize();

        $this->searchFields = [
            'applyType' => [
                'label'     => '',
                'field'     => 'applyType',
                'type'      => 'select',
                'disabled'  => false,
                'data' => [
                    '0' => '适用病虫害',
                    '1' => '病害',
                    '2' => '虫害',
                ]
            ],
            'name' => [
                'label'     => '名称',
                'field'     => 'name',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'
            ],
        ];
    }

    public function index() {
        $request = $this->request;
        if($request->isAjax()){
            $param = $request->param();
            $model = model($this->model);

            // 每页起始条数
            $start = $param['start'];
            // 每页显示条数
            $length = $param['length'];
            // 排序条件
            $columns = $param['order'][0]['column'];
            $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

            $where = $this->getFilterWhere($request);
            if($this->defaultWhere) {
                $where = array_merge($where, $this->defaultWhere);
            }

            $list = $model->where($where)->limit($start, $length)->order($order)->select();
            foreach ($list as $item) {
                if ($item->applyHarm === '1') {
                    $applyHarm = '病害';
                } elseif ($item->applyHarm === '2') {
                    $applyHarm = '虫害';
                } else {
                    $applyHarm = '病害和虫害';
                }
                $item->applyHarm = $applyHarm;
            }
            $count = $model->where($where)->count();

            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count,
            ];

            return json($result);
        }

        return parent::index();
    }

    public function add(Request $request) {

        return parent::add($request);
    }

    public function addPost(Request $request, $redirect = '') {
        $params = $request->param();
        if(is_array($params['applyHarm'])){
            $request->post(['applyHarm' => implode(',', $params['applyHarm'])]);
        }

        return parent::addPost($request, $this->redirect);
    }

    public function edit(Request $request) {

        return parent::edit($request);
    }

    public function editPost(Request $request, $redirect = '') {
        $params = $request->param();
        if(is_array($params['applyHarm'])){
            $request->post(['applyHarm' => implode(',', $params['applyHarm'])]);
        }

        return parent::editPost($request, $this->redirect);
    }

    public function delete() {
        return parent::delete();
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['applyType']) && $filter['applyType']){
                $where['applyHarm'] = ['like', '%'.$filter['applyType'].'%'];
            }
            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        return $where;
    }

}
